﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BTN04.BUS;
using BTN04.DTO;
using System.Collections.Generic;

namespace BTN04.Template
{
    public partial class GUEST : System.Web.UI.MasterPage
    {
        private SanPhamCrt spCrt=new SanPhamCrt ();
        protected void Page_Load(object sender, EventArgs e)
        {

           txtMatKhau.Text = "123456";
            if (!IsPostBack)
            {
                LoaiSanPhamBUS lspBUS = new LoaiSanPhamBUS();
                List<LoaiSanPhamDTO> _lstLoaiSanPham = new List<LoaiSanPhamDTO>();
                _lstLoaiSanPham = lspBUS.DanhSachLoaiSanPham();
                ddlListLoaiSanPham.DataSource = _lstLoaiSanPham;
                ddlListLoaiSanPham.DataMember = "MaLoaiSanPham";
                LoaiSanPhamDTO lspSTO = new LoaiSanPhamDTO();
                lspSTO.MaLoaiSanPham = 0;
                lspSTO.TenLoaiSanPham = "-- Chọn Loại San Phẩm --";
                _lstLoaiSanPham.Insert(0, lspSTO);
                ListItem item;//= new ListItem();
                for (int i = 0; i < _lstLoaiSanPham.Count; i++)
                {
                    item = new ListItem();
                    item.Text = _lstLoaiSanPham[i].TenLoaiSanPham;
                    item.Value =_lstLoaiSanPham[i].MaLoaiSanPham.ToString () ;
                    ddlListLoaiSanPham.Items.Add(item);
                }
            }
        }

        public void Update()
        {
            if (txtTenSanPham.Text == "Nhập tên cần tìm" || txtTenSanPham.Text == "")
                spCrt.ChkTen = false;
            else
            {
                spCrt.TenSanPham = txtTenSanPham.Text;
                spCrt.ChkTen = true;
            }
            spCrt .MaLoaiSanPham =int.Parse (ddlListLoaiSanPham.SelectedValue.ToString());
            if (spCrt.MaLoaiSanPham != 0)
            {
                spCrt.MaLoaiSanPham = int.Parse(ddlListLoaiSanPham.SelectedValue);
                spCrt.ChkLoaiSanPham = true;
            }
            if (txtGiaTu.Text != "" && txtGiaDen.Text != "")
            {
                spCrt.GiaTu = float.Parse(txtGiaTu.Text);
                spCrt.GiaDen = float.Parse(txtGiaDen.Text);
                spCrt.ChkGia = true;
            }
            else
            {
                spCrt.GiaTu = 0;
                spCrt.GiaDen = 0;
                spCrt.ChkGia = false;
            }
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Session["tensanpham"] = txtTenSanPham.Text;
            Session["malsp"] = ddlListLoaiSanPham.SelectedValue;
            if (txtGiaTu.Text != "")
                Session["giatu"] = txtGiaTu.Text;
            else
                Session["giatu"] = 0;
            if (txtGiaDen.Text != "")
                Session["giaden"] = txtGiaDen.Text;
            else
                Session["giaden"] = 0;
            Response.Redirect("../GUEST/TimKiemSanPham.aspx");
        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            string tendangnhap = txtTenDangNhap.Text;
            string matkhau = txtMatKhau.Text;
            NguoiDungDTO ndDTO = new NguoiDungDTO();
            ndDTO.TenDangNhap = tendangnhap;
            ndDTO.MatKhau = matkhau;
            NguoiDungBUS ndBUS = new NguoiDungBUS();
            DataTable dt = new DataTable();
            if (tendangnhap.Length != 0)
            {
                dt = ndBUS.KiemTraDangNhap(tendangnhap, matkhau);
                
                if(dt.Rows.Count != 0)
                {
                    Session["tennguoidung"] = dt.Rows[0]["HoTen"];
                    Session["tendangnhap"] = tendangnhap;
                    Session["tendn"] = tendangnhap;
                    //txtTenDangNhap.Text = Session["tendangnhap"].ToString()+" m d";

                    ndDTO.InitCart();
                    CurrentContext.NguoiDungDTO = ndDTO;

                    if (Convert.ToInt32(dt.Rows[0]["MaNguoiDung"]) == 1)
                    {
                        Response.Redirect("~/ADMIN/trangchu.aspx");
                    }
                    if (Convert.ToInt32(dt.Rows[0]["MaNguoiDung"]) == 2)
                    {
                        Response.Redirect("~/USER/trangchu.aspx");
                    }
                    
                }
              
            }
            
        }

        protected void btnDangKy_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/GUEST/dangky.aspx");
        }

      




    }
}
